/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servidorbancos.OAUTH;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.scribe.model.Response;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.FacebookApi;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import servidorbancos.resources.Agencia;

/**
 *
 * @author Eric
 */
public class RedeSocial {
 
    private static final String PROTECTED_RESOURCE_URL = "http://api.twitter.com/1/statuses/update.xml";
    private static final String PROTECTED_RESOURCE_FOLLOWERS = "http://api.twitter.com/1/followers/ids.xml";
    private static final String PROTECTED_RESOURCE_TIMELINE = "http://api.twitter.com/1/statuses/home_timeline.xml";

    
   

  public void PrintaAgencias(ArrayList<Agencia> agencias)
  {
        int i = 1;
        int sizeAgencias = agencias.size();

     while(agencias.size() >= i){  
        OAuthService service = new ServiceBuilder()
                                    .provider(TwitterApi.class)
                                    .apiKey("tQN6jLfZzmJqlL0rADA")
                                    .apiSecret("fdEc0cqTEaKgpFlUpOo5Hln9XAu0v89v2tEfDDkd8")
                                    .build();
        Scanner in = new Scanner(System.in);

        System.out.println("=== Twitter's OAuth Workflow ===");
        System.out.println();

        // Obtain the Request Token
        System.out.println("Fetching the Request Token...");
        Token requestToken = service.getRequestToken();
        System.out.println("Got the Request Token!");
        System.out.println();

        System.out.println("Now go and authorize Scribe here:");
        System.out.println(service.getAuthorizationUrl(requestToken));
        System.out.println("And paste the verifier here");
        System.out.print(">>");
        Verifier verifier = new Verifier(in.nextLine());
        System.out.println();

        // Trade the Request Token and Verfier for the Access Token
        System.out.println("Trading the Request Token for an Access Token...");
        Token accessToken = service.getAccessToken(requestToken, verifier);
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: " + accessToken + " )");
        System.out.println();

        String Sfinal = "";

        
        while(i % 4 != 0 && agencias.size() >= i){
        
                Sfinal = Sfinal + "A:" + agencias.get(i -1).getBanco();
                Sfinal = Sfinal + "L:" + agencias.get(i - 1).getLat();
                Sfinal = Sfinal + "LO:" + agencias.get(i - 1).getBanco();
                Sfinal = Sfinal + "D:" + agencias.get(i - 1).getDistancia();
                Sfinal = Sfinal + " | ";
          
                i++;            
        }

        if(agencias.size() >= i){
                Sfinal = Sfinal + "A:" + agencias.get(i -1).getBanco();
                Sfinal = Sfinal + "L:" + agencias.get(i - 1).getLat();
                Sfinal = Sfinal + "LO:" + agencias.get(i - 1).getBanco();
                Sfinal = Sfinal + "D:" + agencias.get(i - 1).getDistancia();
                Sfinal = Sfinal + " | ";
        }
                
        // Now let's go and ask for a protected resource!
        System.out.println("Now we're going to access a protected resource...");
        OAuthRequest request = new OAuthRequest(Verb.POST,  PROTECTED_RESOURCE_URL);
        request.addBodyParameter("status", Sfinal);
        service.signRequest(accessToken, request);

        Response response = request.send();
        System.out.println("Postando no seu twitter");
        System.out.println();
        System.out.println(response.getBody());
        
        i++;
    }
    
    
    /*OAuthRequest getfollowersids = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_FOLLOWERS);
    service.signRequest(accessToken, getfollowersids);

    OAuthRequest gettimeline = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_TIMELINE);
    service.signRequest(accessToken, gettimeline);
    */

    /*Response response1 = getfollowersids.send();
    System.out.println("Pegando seus amigos");
    System.out.println();
    System.out.println(response1.getBody());

    Response response2 = gettimeline.send();
    System.out.println("Pegando sua timeline");
    System.out.println();
    System.out.println(response2.getBody());*/

    System.out.println();
    System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
  }


}